﻿var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';

var that;
var app = getApp();
Page( 
	{
data:{
	u:false,
	l:false,
	r:false,
	bu:false,
	bl:false,
	br:false,
	squ:true,
	sqq:true,
	url_https:"",
	tmp:{}
},
onLoad: function (options) {
    that=this;
    that.data.url_https=options.url;
    if (!util.judge.isNull(options.p)) {
	  that.setData({
		  squ:false
	  });
  }else{
	  that.setData({
		  sqq:false
	  });
  }

  	util.showLoading();
  	wxApi.wxLogin()()
    .then(res => {
        console.log('1.成功了');
        console.log(res.code);
        var url = that.data.url_https + config.url_login;
        var params = {
            code: res.code
        }
        //2.获取openid
        return wxRequest.postRequest(url, params);
    }) 
	.then(res => {
    	console.log('4.成功了');
    	console.log(res);

    	wx.setStorageSync('sid', res.sessionId);
    	wx.setStorageSync('key', res.sessionKey);

    	// var url = that.data.url_https + '/WxOpen/DecodeEncryptedData';
    	// var params = {
    	// 	'type':"userInfo",
		// 	sessionId: wx.getStorageSync('sid'),
		// 	sessionKey: wx.getStorageSync('key'),
    	// 	encryptedData:resddd.encryptedData,
    	// 	iv:resddd.iv
    	// }
    	// return wxRequest.postRequest(url, params);
	})
	.catch(res => {
        console.log('7.err-' + util.objtostr(res));
        util.alert(util.objtostr(res));
    })
	.finally(function (res) {
	   util.hideLoading();
		console.log('app.getuser.finally~')
	});

} 
,bugg1:function(e){
	wx.getWeRunData({
	  success (res) {
		console.log(res);
			that.setData({
			  r:true
		  });
		  if( that.data.u == true && that.data.l==true && that.data.r == true ){
					
                    wx.switchTab({
						url: '../index/index',
						success: function (e) {
							var page = getCurrentPages().pop();
							if (page == undefined || page == null) return;
							page.onShow();
						}
					})
              
                }
	  }
	  ,fail:function(res){
		  
		  console.log(res);
	  }
	})
	
}
,bugg2:function(e){
	wx.getLocation({
	  success (res) {
		console.log(res);
			that.setData({
			  l:true

		  });
		  if( that.data.u == true && that.data.l==true && that.data.r == true ){
					
                    wx.switchTab({
						url: '../index/index',
						success: function (e) {
							var page = getCurrentPages().pop();
							if (page == undefined || page == null) return;
							page.onShow();
						}
					})
              
                }
	  } ,fail:function(res){
		  
		  console.log(res);
	  }
	})
}
, bugg: function (e) {
    util.showLoading();

    var resddd = e.detail;
    wx.setStorageSync('iv', resddd.iv);
	var url = that.data.url_https + '/WxOpen/DecodeEncryptedData';
	var params = {
		'type':"userInfo",
		sessionId: wx.getStorageSync('sid'),
		sessionKey: wx.getStorageSync('key'),
		encryptedData:resddd.encryptedData,
		iv:resddd.iv
	}
	wxRequest.postRequest(url, params)
	.then(res => {
		console.log('4.成功了');
		console.log(res);
			   
		that.data.tmp = res;
		var url = config.ulr_signin;
		var params = {
		    openid: that.data.tmp.openId,
		    nickname:that.data.tmp.nickName,
		    imgurl:that.data.tmp.avatarUrl,
		    unionId:that.data.tmp.unionId
		}
		return wxRequest.postRequest(url, params);
	})
    .then(res=>{
        console.log('5.成功了');
        if(!util.judge.isObject(res)){
            throw util.stop();
        }

        var isnew = res.istbkb;
        that.data.isnew=isnew;

        if(res.id ==0){
            throw util.stop();
        }

        var obj ={};
        obj.openid = that.data.tmp.openId;
        obj.name = that.data.tmp.nickName;
        obj.img = that.data.tmp.avatarUrl;
        obj.unionId= that.data.tmp.unionId;
        obj.uid = res.id;
        obj.isgl= res.isgl;
        obj.isadmin= res.isadmin;
        obj.isgly= res.isgly;
        obj.code = res.code;
        that.data.user = obj;
        wx.setStorageSync('uid', obj.uid);
        wx.setStorageSync('data', that.data);
		   
		app.globalData.user=that.data.user;
		app.globalData.isnew= that.data.isnew;
		app.globalData.sxindex=1;


        console.log('6.成功了');
        try {
            wx.getSystemInfo({
                success: function(res) {
                    console.log(res)
                    var url = config.url_saveSystemInfo;
                    var params = {
                        data: JSON.stringify(res),
                        myid:obj.uid
                    }
                    wxRequest.postRequest(url, params);
                }
            })
        } catch (e) {
            console.error('getSystemInfoSync failed!');
        }

		//刷新主页

        wx.switchTab({
            url: '../index/index',
            success: function (e) {
				
            }
        });
    })
    .catch(res => {
        console.log('7.err-' + util.objtostr(res));
		util.alert(util.objtostr(res));
    })
	.finally(function (res) {
	   
		console.log('app.getuser.finally~')
	});
               
}
,onNotLogin:function(){
	wx.switchTab({
		url: '../index/index',
		success: function (e) {

		}
	});
}
,callback:function(e){
	
		var u = e.detail.authSetting['scope.userInfo'];
		var l = e.detail.authSetting['scope.userLocation'];
        var r = e.detail.authSetting['scope.werun']; //|| r== false || util.judge.isNull(r)
        if ( u== false || util.judge.isNull(u) || l== false || util.judge.isNull(l) ) {
			util.alert("请确保同意小程序获取您的用户资料、地理位置、运动步数");
		}else{
			wx.switchTab({
				url: '../index/index',
				success: function (e) {
					var page = getCurrentPages().pop();
					if (page == undefined || page == null) return;
					page.onShow();
				}
			})
		}
	}
	
	 
	
}	
);